package com.lza.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.xiaoymin.knife4j.core.util.StrUtil;
import com.lza.redis.RedisUtil;
import com.lza.response.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import sun.plugin.liveconnect.SecurityContextHelper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;
/*
注销的Handler,不是注销成功的handler
 */
@Component
public class LogoutHandler extends JSONAuthentication implements org.springframework.security.web.authentication.logout.LogoutHandler {
    @Override
    public void logout(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) {
        httpServletResponse.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("application/json");
        // redisUtil.expire("token", 0, TimeUnit.MICROSECONDS);//设置马上过期
        String headerToken = httpServletRequest.getHeader("Authorization");
        System.out.println("logout header Token=" + headerToken);
        System.out.println("logout request getMethod=" + httpServletRequest.getMethod());
        if (!StringUtils.isEmpty(headerToken)) {
            //postMan测试时，自动加入的前缀，要去掉。
            String token = headerToken.replace("Bearer", "").trim();
            SecurityContextHolder.clearContext();//清除登陆信息
           // System.out.println("authentication="+authentication);
        }
    }
}
